home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMIBEST1.ADF / AmigaBasicStuff / AutoRequester < prev    next >
Text File  |  1987-07-22  |  11KB  |  199 lines

  1. AutoRequester in AmigaBASIC - Ervin Thompson [70567,523]
  2.  
  3. While the AutoRequester is a fairly simple call from BASIC using the
  4. LIBRARY functions, it does require the setup of three IntuiText structures
  5. in order to work. This "tutorial" will show you how to create your own
  6. AutoRequester and give a brief explanation of the structures used in the
  7. Amiga operating system.
  8.  
  9. The IntuiText structure is perhaps one of the most commonly used
  10. structures when programming under Intuition. For those of you unfamiliar
  11. with the term "structure" in this context, perhaps a brief explanation
  12. would be in order.  
  13.  
  14. In many programming envoirnments the programmer places various values in
  15. fixed memory locations in order to achieve the desired result. This may be
  16. as simple as changing the screen color or as complex as drawing an entire
  17. screen bit by bit.  The multitasking nature of the Amiga prohibits the use
  18. of most fixed memory locations in favor of tables of data that the
  19. operating system is pointed toward in order to get the data it needs to
  20. perform a given task.  These tables of data are known as structures.  They
  21. more often than not contain variable data of differing types.  Using the
  22. AutoRequester as an example we'll see that a structure can contain byte
  23. values(8 bits), word values (2 bytes or 16 bits), and longword values (4
  24. bytes or 32 bits). The actual "byte" size of the structure is fixed -
  25. although it's location in memory is not.  Many times the structure
  26. contains pointers to strings or other structures.  Pointers are simply
  27. memory locations that contain the address of data or data structures.
  28.  
  29. We'll need to "import" two functions:  the AutoRequest function from the
  30. Intuition.library and the AllocMem function from the Exec.library. We'll
  31. also be using the FreeMem function from the Exec.library but it returns no
  32. value so we need not DECLARE it.  Only functions that return a value to
  33. AmigaBASIC need be DECLAREd.  We'll be using the AllocMem& function to
  34. allocate enough memory for each of the three IntuiText structures
  35. AutoRequest will need. AutoRequest then takes the data we've provided in
  36. the structures and creates a Requester we can call at will.  The Requester
  37. prompts the user for a yes/no type answer and then returns a BOOLEAN
  38. response to the program.
  39.  
  40. You may be asking "Why all these text structures? Can't we just PRINT
  41. something?". It would be nice if Intuition operated so simply but it
  42. doesn't. Many of Intuition functions require a close working relationship
  43. with the Graphics.library to perform screen output. The IntuiText
  44. structure is used to communicate to Intuition the what and where (and some
  45. other info) for the text we would like to display.
  46.  
  47. Now we'll go step by step through the IntuiText structure and any related
  48. functions. You'll notice I refer to some of the variable names differently
  49. than you may have seen in similar "C" applications. I use TDI's Modula-2
  50. and it doesn't allow the underline character so I'm not at all accustomed
  51. to using it. Some of these are self-explanatory but I'll mention them
  52. anyway.
  53.  
  54. AllocMem& and FreeMem
  55.  
  56. The IntuiText structure requires 20 bytes of memory in which to reside.
  57. How did we determine that this was the correct byte count? You need to add
  58. all the bytes required in the structure in order to protect this area. We
  59. call a system function, AllocMem, which, as the name implies, allocates a
  60. block of memory. It also returns a pointer to that block. We need the
  61. pointer so we can create and then POKE some values into our structure. The
  62. system also wants to know just what type of memory we are requesting. The
  63. ClearPublic& (I stole this one) variable is assigned the value of 65537.
  64. This tells Intuition we want the data structure to reside in public memory
  65. and we want Intuition to clear the memory and reset all the memory
  66. locations to a value of 0, or null. That way we start with a clean slate.
  67. Our pointer, which we "ingeniously" called Ptr&, will be used as the
  68. baseline in order to POKE values into the structure. If the AllocMem&
  69. function returns a value of 0 then the system was unable to allocate
  70. memory for the structure(s). Proper programming dictates an orderly exit
  71. from the program if this should occur. I didn't do that in the example
  72. program but it should be included in a "real" program.
  73.  
  74. FreeMem simply deallocates the memory we used for our structure and frees
  75. it to the system for other uses.
  76.  
  77. The Structure
  78.  
  79. After some digging around (and getting more than a few crashes) I
  80. discovered how the structure is "looked at" by Intuition. The first three
  81. values - FrontPen, BackPen and DrawMode - hold one byte values. The next
  82. two - LeftEdge and TopEdge - hold word (or two-byte) values while the
  83. last three - ITextFont, IText (the address of our string), and NextText -
  84. hold longword (or four- byte) values. If you add all these together you
  85. have a total of 19. The only problem is that we can only poke word values
  86. into even addresses and longwords must be on even 4-byte multiples. As it
  87. turns out the last odd byte (or bytes, in some cases) is "wasted" by the
  88. system. This comes into play in the IntuiText structure. The first three
  89. values are single bytes so the fourth is then wasted in order to put
  90. everything back in line.
  91.  
  92. The first three byte values we'll POKE in - FrontPen, BackPen, and
  93. DrawMode - will control the colors of our displayed text. The FrontPen
  94. and Backpen colors are those selected in Preferences or as altered by
  95. your BASIC program. The DrawMode parameter - Jam2, Complement,
  96. InverseVid, or any combination of the three - adds a special touch to our
  97. text rendition. I've included their respective values in the program
  98. text. The LeftEdge and TopEdge parameters tell Intuition where you want
  99. your text displayed relative to the "containing" element. In this case
  100. the Requester but it could be a Gadget or Window.
  101.  
  102. ItextFont is used to control the font and fontstyle rendering of the text.
  103. I'm using the default font here and for most applications that would
  104. suffice. Using the SetSoftStlye and related functions you can add a
  105. little more flair to your text rendering but that would require
  106. substantially more programming. We'll keep our "simple" Requester simple.
  107.  
  108. The SADD function is used to extract our string address. This is
  109. recommended in the user's manual over the VARPTR function.
  110.  
  111. The last parameter, NextText, is simply a pointer to another IntuiText
  112. structure, if you so wish. It is NOT used to link in the other two
  113. structures we are initializing here. It is used to tie together text into a
  114. single unit. As you can see each structure only allows for that string of
  115. text to be printed in one color or be located at one place in the
  116. containing element. By linking structures together we can get a little
  117. fancier with our text.
  118.  
  119. Many other structures, such as the Gadget or reqular Requester, can be
  120. used in your AmigaBASIC programs. There are no hard, fast rules
  121. concerning their setup as long as you POKE the right info into the
  122. structure. You can't, for example, POKE a word or long word into an odd
  123. memory address (in this case AmigaBASIC will give you an error). The
  124. texts I've been using are the ROM Kernal Manuals and the Amiga
  125. Programmers Handbook (SYBEX). The Programmers Handbook, while being
  126. somewhat terse at times, does include descriptions of most of the system
  127. calls and describes the various structures used in them. The ROM Kernal
  128. Manuals give a detailed explanation of the calls. Neither are geared
  129. toward the BASIC programmer but once you get the hang of it you can use
  130. them quite easily. One book that I've seen but have yet to acquire is
  131. Advanced AmigaBASIC published by COMPUTE!. This seems to be an excellent
  132. text. It covers a broad range of AmigaBASIC features, including a large
  133. number of LIBRARY calls.
  134.  
  135. If you should expand your programming horizons in the future to include
  136. one of the high level compiled languages such as C or Modula-2, I hope
  137. this mini-tutorial will be an aid.
  138.  
  139. (NOTE: I do a "column" called WEEKLY on the Amiga BBS of Denver 
  140. (303-693-4735). The REMS were added due to TBBS's "desire" to shrink short
  141. lines into a single line. This example program should run as written,
  142. though.)
  143.  
  144. ' This demos the Intuition "AutoRequest" function                   .
  145.  
  146. DECLARE FUNCTION AutoRequest&() LIBRARY '                           .
  147. DECLARE FUNCTION AllocMem&() LIBRARY '                              .
  148.  
  149. ' These .library files must be in the current directory or you must .
  150. ' add a "CHDIR" to the directory containing them                    .
  151.  
  152. LIBRARY "intuition.library" '                                       .
  153. LIBRARY "graphics.library" '                                        .
  154. LIBRARY "exec.library" '                                            .
  155.  
  156. ' The following initializes the constants Intuition needs to draw the
  157. ' AutoRequester                                                     .
  158. W& = WINDOW(7) ' Get a pointer to the current window                .
  159. Text$ = "Display This Requester Again?"+CHR$(0) '                   .
  160. PosText$ = "Yesiree, Bob"+CHR$(0) '                                 .
  161. NegText$ = "Noway, Jack"+CHR$(0) '                                  .
  162. w% = 300 ' the width of our Requester                               .
  163. h% = 50 ' the height                                                .
  164. PFlags& = 0 ' These can be used to let Intuition know which other   .
  165. NFlags& = 0 ' events, such as inserting a disk, will also satisfy   .
  166.             ' the Requester                                         .
  167.  
  168. 'this is is simpler than using three separate IntuiText structures  .
  169. CALL SetTextStructure(Text&,0,1,1,25,5,Text$) '                     .
  170. CALL SetTextStructure(PText&,0,1,1,6,3,PosText$) '                  .
  171. CALL SetTextStructure(NText&,0,1,1,6,3,NegText$) '                  .
  172.  
  173. DisplayRequester: '                                                 .
  174. bool = (AutoRequest&(W&,Text&,PText&,NText&,PFlags&,NFlags&,W%,H%))'.
  175. IF bool = 1 THEN GOTO DisplayRequester '                            .
  176. IF bool = 0 THEN '                                                  .
  177.   CALL FreeMem(Text&,20)  ' This deallocates the memory for         .
  178.   CALL FreeMem(PText&,20) ' the IntuiText structures                .
  179.   CALL FreeMem(NText&,20) '                                         .
  180. END IF '                                                            .
  181. END '                                                               .
  182.  
  183. ' This is the IntuiText structure                                   .
  184. SUB SetTextStructure(Ptr&,fp%,bp%,dm%,x%,y%,strng$) STATIC '        .
  185.   ClearPublic& = 65537& '                                           .
  186.   Ptr& = AllocMem&(20,ClearPublic&) ' This is the "Question"        .
  187.   IF Ptr& <> 0 THEN '                                               .
  188.     POKE(Ptr&),fp%   ' FrontPen (value = 1-4)                       .
  189.     POKE(Ptr&+1),bp% ' BackPen  (value = 1-4)                       .
  190.     POKE(Ptr&+2),dm% ' DrawMode (1=Jam2,2=Complement,4=InverseVid)  .
  191.     POKEW(Ptr&+4),x% ' LeftEdge                                     .
  192.     POKEW(Ptr&+6),y% ' TopEdge                                      .
  193.     POKEL(Ptr&+8),0  'ITextFont (0 = default font)                  .
  194.     POKEL(Ptr&+12),SADD(strng$) ' IText (address of our string)     .
  195.     POKEL(Ptr&+16),0 ' NextText                                     .
  196.   END IF '                                                          .
  197. END SUB '                                                           .
  198.  
  199.